<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 17.1.&nbsp; Finding Help on the Web</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-17.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-17-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><TD valign="top"><a name="learnphpmysql-CHP-17-SECT-1"></a>
<h3 id="title-IDAF0NTR" class="docSection1Title">17.1. Finding Help on the Web</h3>
<a name="IDX-CHP-17-0693"></a> 

<p class="docText">The Web contains a plethora of information. Remember both PHP and MySQL are open source technologies, supported by a community of developers who share their work. That means that code is readily available on the Web for your use. There may be some code glitches, but there is help on the Web when you type your query into a search engine. We prefer Google, but you can use Yahoo! or even MSN to find useful links.</p>
<p class="docText">First of all, you should download the PHP manual available at <a class="docLink" target="_blank" href="http://www.php.net/docs">http://www.php.net/docs</a>. The manual is pretty cool, since you can rapidly access any page of it if you're looking for a particular function by going to <a class="docLink" target="_blank" href="http://www.php.net/">http://www.php.net/</a><tt><i>function_name</i></tt> (you'll fill in the italicized function name you're searching for). There is also a search utility for functions, so you don't need to always fill in the full function name.</p>
<p class="docText">One thing we haven't covered is the PHP Coding Standard. As you've probably guessed, this standard is a document that shows proper format and syntax for variable names, control structures, and much more. These format and syntax recommendations help you minimize coding errors. Currently, some sites that address this include:</p>
<ul><li><p class="docList"><a class="docLink" target="_blank" href="http://srparish.net/writings/php_code_standards.html">http://srparish.net/writings/php_code_standards.html</a></p></li><li><p class="docList"><a class="docLink" target="_blank" href="http://www.phpfreaks.com/tutorials/35/0.php">http://www.phpfreaks.com/tutorials/35/0.php</a></P></li><li><p class="docList"><a class="docLink" target="_blank" href="http://www.phpcommunity.org/node/139">http://www.phpcommunity.org/node/139</a></P></LI></ul>
<p class="docText">We're going to do a minor recap of some of the important concepts covered throughout the book along with code examples. This is just a refresher to jog your brain into remembering a lot of the content you've already digested.</P>
<a name="learnphpmysql-CHP-17-SECT-1.1"></a>
<h4 id="title-IDAX1NTR" class="docSection2Title">17.1.1. Comments</h4>
<p class="docText">Some basic coding standards are comments<a name="IDX-CHP-17-0694"></a> 
 that help you remember what your code is doing. You may need to go back and look at code you wrote several months ago. What seems straightforward now may take considerable time to discern later without leaving some meaningful explanations. Remember that PHP uses the same style for comments as C++, including <tt>/* */</tt> and <tt>//</tt> for a single-line comment.</p>
<P><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><TD><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></TD><TD valign="top">
<p class="docText">Remember every file you create needs to start with a comment block.</p>
</TD></tr></table></td></TR></table></P><br>
<p class="docText">The comment block should include the file's description, version, author, and perhaps a copyright. It can look like <a class="docLink" href="#learnphpmysql-CHP-17-EX-1">Example 17-1</a>.</p>
<a name="learnphpmysql-CHP-17-EX-1"></a><h5 id="title-IDAX2NTR" class="docExampleTitle">Example 17-1. File comments</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
/*
 *
 * this file is about furniture stores.
 * this file is about furniture stores in Minnesota, Wisconsion, Iowa and Illinois.
 *
 * Portions Copyright 2005-2006 (c) O'Reilly &amp; Associates
 * The rest Copyright 2005 (c) from their respective authors
 *
 * @version   $Id: coding_standards.html,v 1.2 2005/12/19 24:49:50
 *
 */
</pre><br>

</td></TR></table></P>
<p class="docText">Files should have comments, and every function should have a block comment specifying the name, parameters, return values, purpose, and last change date, as shown in <a class="docLink" href="#learnphpmysql-CHP-17-EX-2">Example 17-2</a>.</p>
<a name="learnphpmysql-CHP-17-EX-2"></a><H5 id="title-IDAI3NTR" class="docExampleTitle">Example 17-2. Function comments</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
/*
 * furniture stores locator.
 * Locate furniture stores in Minnesota, Wisconsion, Iowa and
 * Illinois based on their zip code.
 *
 * @author    michele davis mdavis@example.com
 * @param     zipcode  the zipcode to search for stores near
 * @return    store     the store id of the nearest store
 * @date      2005-12-21
*
*/
</pre><br>

</td></tr></table></p>
<p class="docText">The first line should be a short description, with the second line providing more details.</p>

<a name="learnphpmysql-CHP-17-SECT-1.2"></a>
<h4 id="title-IDAU3NTR" class="docSection2Title">17.1.2. Formatting</h4>
<a name="IDX-CHP-17-0695"></a> 

<p class="docText">While there are different acceptable styles for name and spacing, the most important thing is that you pick one and stick with it so that your code has consistent visual indicators to anyone who may work with it.</p>
<a name="learnphpmysql-CHP-17-SECT-1.2.1"></a>
<H5 id="title-IDAB4NTR" class="docSection3Title">17.1.2.1. Indenting</h5>
<p class="docText">Some people use tabs to indent, while others use spaces. If you do use spaces, make sure you use a consistent number of spaces; for example, two for each indent. You should indent any time you use a statement that contains a block of code, such as an <tt>if</tt> statement or a <tt>for</tt> loop. This will help you tell which block a statement belongs to and match it to closing brackets (<tt>}</tt>). Indenting isn't always necessaryit's pretty much a personal preference. As you may have noticed, not all our code in this book is indented. This is our preference, since indenting<a name="IDX-CHP-17-0696"></a> 
 or not indenting doesn't change the code.</p>

<a name="learnphpmysql-CHP-17-SECT-1.2.2"></a>
<H5 id="title-IDAV4NTR" class="docSection3Title">17.1.2.2. PHP tags</H5>
<a name="IDX-CHP-17-0697"></a> 
<a name="IDX-CHP-17-0698"></a> 

<p class="docText">You should always use <tt>&lt;?php</tt> and <tt>?&gt;</tt> to delimit your PHP code. This is the most portable and supported format. Don't use the older <tt>&lt;?</tt> and <tt>?&gt;</tt> tags, as they're not fully supported and can confuse XML parsers.</p>

<a name="learnphpmysql-CHP-17-SECT-1.2.3"></a>
<H5 id="title-IDAQ5NTR" class="docSection3Title">17.1.2.3. Templating</h5>
<p class="docText">We've used the Smarty templating<a name="IDX-CHP-17-0699"></a> 
<a name="IDX-CHP-17-0700"></a> 
 system in many examples in this book. Smarty offers a nice mix between easy to use and flexible, but there are other templating systems you can use. Their use is highly recommended, as is placing the template files in a separate directory from the PHP code. Using PHP to gather and validate data, and then using a template system to display the results, maintains cleaner, easier-to-maintain code.</p>

<a name="learnphpmysql-CHP-17-SECT-1.2.4"></a>
<h5 id="title-IDAGAOTR" class="docSection3Title">17.1.2.4. Expressions</H5>
<p class="docText">Complex expressions<a name="IDX-CHP-17-0701"></a> 
<a name="IDX-CHP-17-0702"></a> 
 can be difficult to decipher, but there are some guidelines to make them easier to understand:</p>
<UL><li><p class="docList">You can always use extra parentheses to make the order of evaluation clearer in expressions or to eliminate any gray areas.</p></LI><LI><p class="docList">Keep it simple; if an evaluation is very complex, split it up into manageable chunks.</p></LI><li><p class="docList">The not (<tt>!</tt>) operator can make expressions difficult to read, so try to eliminate it.</p></LI><LI><p class="docList">Use multiple <tt>if else</tt> statements instead of the ternary operator (<tt>x ? condition : condition</tt>), because it's more concise, but it's also harder to read.</p></li></ul>

<a name="learnphpmysql-CHP-17-SECT-1.2.5"></a>
<h5 id="title-IDARBOTR" class="docSection3Title">17.1.2.5. Function calls</H5>
<a name="IDX-CHP-17-0703"></a> 
<a name="IDX-CHP-17-0704"></a> 
<a name="IDX-CHP-17-0705"></a> 
<a name="IDX-CHP-17-0706"></a> 
<a name="IDX-CHP-17-0707"></a> 

<p class="docText">Add only one space after a comma in a parameter listing, as shown below:</p>
<pre>
$var = inventory($location, $category);
</pre><BR>

<p class="docText">When assigning values, place one space before and one after the equals (<tt>=</tt>) sign. You can add more spaces when assigning multiple values to make them easier to read.</p>
<pre>
$count        = inventory("Minneapolis","home");
$count2       = inventory("Chicago","office");
</pre><br>


<a name="learnphpmysql-CHP-17-SECT-1.2.6"></a>
<H5 id="title-IDAZCOTR" class="docSection3Title">17.1.2.6. Function definitions</H5>
<p class="docText">Functions should be defined with an indent after the function line, and any included code blocks, such as the <tt>if</tt> statement, should be indented again.</p>
<pre>
function inventory($location, $category = 'office') {
    if (condition) {
        statement;
    }
    return $return_value;
}
</pre><BR>

<p class="docText">If you have arguments with default values, place them at the end of the argument list. The return value from your function should indicate whether it was successful or whether there's a chance it may fail.</p>
<pre>
function inventory($location, $category = 'office') {
    if (!$location) {
        $return_value=false;
    }
    return $return_value;
</pre><br>



<a name="learnphpmysql-CHP-17-SECT-1.3"></a>
<h4 id="title-IDAMDOTR" class="docSection2Title">17.1.3. Objects</h4>
<p class="docText">Objects have general design rules to aid their design and use because of their complexity. Each object should only have attributes associated with it that are directly related to the object. Each object should have its own error handling defined so that errors don't need to propagate to higher level objects<a name="IDX-CHP-17-0708"></a> 
 that likely don't know as much about the environment in which the error occurred. Likewise, all objects should have their own constructor methods.</p>

<a name="learnphpmysql-CHP-17-SECT-1.4"></a>
<h4 id="title-IDA0DOTR" class="docSection2Title">17.1.4. Naming</h4>
<p class="docText">Here are some guidelines for naming:</p>
<ul><li><p class="docList">Name your functions to indicate what they do; for example, <tt>connectDatabase, deleteUser</tt>.</p></li><LI><p class="docList">Name your variables to indicate what they store; for example, <tt>DatabaseName, RowCount</tt>.</p></li><LI><p class="docList">Name constants using uppercase descriptive words with underscores to separate words. If the constant belongs to an object, prefix the constant with the name of the package.</P></li><LI><p class="docList">Abbreviations are OK as long as they're used consistently and aren't too difficult to interpret.</p></li><li><p class="docList">Global variables should use longer names than local variables do.</P></li></UL>

<a name="learnphpmysql-CHP-17-SECT-1.5"></a>
<h4 id="title-IDA2EOTR" class="docSection2Title">17.1.5. Control Structures</h4>
<a name="IDX-CHP-17-0709"></a> 

<p class="docText">Control structures include <tt>if, for, while</tt>, and <tt>switch</tt>. You should indent a couple of spaces for the <tt>if</tt> statement. There should be one space between the statement and the opening parenthesis for the expression. This helps differentiate them visually from function calls. For example:</P>
<pre>
if ((expression1) || (expression2)) {
    do_something;
} elseif ((expression3) &amp;&amp; (expression4)) {
    do_something_else;
} else {
    do_default;
}
</pre><BR>

<p class="docText">Curly braces (<tt>{}</tt>) make reading your code easier and help reduce errors. Use them even if you have only one statement to execute in the block.</p>
<p class="docText">Here's an example for <tt>switch</tt> statements:</P>
<pre>
switch (expression) {
    case 1: {
        do_something_1;
        break;
    }
    case 2: {
        do_something_2;
        break;
    }
    default: {
        do_default;
        break;
    }
}
</pre><br>

<a name="learnphpmysql-CHP-17-SECT-1.5.1"></a>
<h5 id="title-IDA3FOTR" class="docSection3Title">17.1.5.1. Including or requiring PHP files</H5>
<p class="docText">When you use <tt>include_once</tt> and <tt>require_once</tt>, they guarantee that the code won't be included more than once. They're smart enough to keep track of which code has been included between them:</P>
<pre>
include_once('example.php');
require_once('example.php');
</pre><br>

<p class="docText">The above example won't include <span class="docEmphasis">example.php</span> more than once.</p>
<p class="docText">Be sure to use <tt>require</tt> when your code can't continue if the file to include is missing. Optional code can and should use <tt>include</tt>.</p>
<pre>
include('optional_functions.php');
</pre><br>




</TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-17.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-17-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
